import request from "@/utils/request";
import { getToken } from "@/utils/cache";

console.log(getToken());
const AuthAPI = {
  /**
   * 发送验证码
   * @param data 包含手机号的对象
   * @returns Promise
   */
  sendVerificationCode(data: { mobile: string }): Promise<void> {
    return request({
      url: '/api/v1/auth/sendVerificationCode',
      method: 'POST',
      data: data
    });
  },
  /**
   * 注册接口
   *
   * @param data 注册数据
   * @returns 返回 token
   */
  register(data: RegisterFormData): Promise<RegisterResult> {
    return request<RegisterResult>({
      url: "/api/v1/auth/doRegister",
      method: "POST",
      data: data,
    });
  },
  /**
   * 登录接口
   *
   * @param username 用户名
   * @param password 密码
   * @returns 返回 token
   */
  login(data: LoginFormData): Promise<LoginResult> {
    console.log("data", data);
    return request<LoginResult>({
      url: "/api/v1/auth/login/member",
      method: "POST",
      data: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    });
  },

  /**
   * 微信登录接口
   *
   * @param code 微信登录code
   * @returns 返回 token
   */
  wechatLogin(code: string): Promise<LoginResult> {
    return request<LoginResult>({
      url: `/api/v1/auth/login/wechat`,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "x-client-type": "MINI",
      },
      data: {
        code: code,
      },
    });
  },

 /**
   * 微信公众号登录接口
   *
   * @param code 微信登录code
   * @returns 返回 token
   */
  wxGongzLogin(code: string): Promise<LoginResult> {
    return request<LoginResult>({
      url: `/api/v1/auth/login/wechat`,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        code: code,
      },
    });
  },

  /**
   * 用户注册信息完善
   * @param data 用户注册信息完善数据
   * @returns 返回 token
   */
  WxCompleteProfile(data: CompleteProfileFormData): Promise<LoginResult> {
    return request<LoginResult>({
      url: `/api/v1/auth/editUser`,
      method: "POST",
      data: data,
    });
  },
  /**
   * 微信登录获取code内容
   * @param code 微信登录code
   * @returns 返回 token
   */
  getlocalCode(data: any) {
    console.log(data, 111111);
    return request({
      url: "/api/v1/auth/login/member",
      method: "POST",
      data: data,
    });
  },

  /**
   * 获取微信登录地址
   * @returns 返回 一个包含微信登录地址的对象
   */
  getWxLoginUrl() {
    return request({
      url: "/api/v1/auth/login/wxlogin-url",
      method: "GET",
      data: {
        redirect: "https://app.jxbylj.com/#/pages/login/wechat-authorize",
      },
    });
  },
  /**
   * 登出接口
   */
  logout(): Promise<void> {
    return request({
      url: "/api/v1/auth/logout",
      method: "DELETE",
    });
  },
};

export default AuthAPI;

/** 登录响应 */
export interface LoginResult {
  /** 访问token */
  accessToken: string;
  /** token 类型 */
  tokenType?: string;
}

/** 注册表单数据 */
export interface RegisterFormData {
  username: string;
  password: string;
  phone: string;
  mobile?: string;
  areaCode?: string;
  verificationCode?: string;
  recommenderId?: string;
}

/** 注册响应 */
export interface RegisterResult {
  /** 访问token */
  accessToken: string;
  /** token 类型 */
  tokenType?: string;
}

export interface LoginFormData {
  username: string;
  password: string;
}

/** 用户信息完善表单数据 */
export interface CompleteProfileFormData {
  username: string;
  mobile: string;
  password: string;
  confirmPassword: string;
  gender: number;
  areaCode: string;
  avatar?: string;
}
